package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Importbenefit;
import com.ktgj.customer.domain.vo.ImportbenefitVo;
import com.ktgj.customer.service.IImportbenefitService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 续费记录
Controller
 * 
 * @author ruoyi
 * @date 2021-07-27
 */
@ControllerAdvice
@RestController
@RequestMapping("/importbenefit")
public class ImportbenefitController extends BaseController
{
    @Autowired
    private IImportbenefitService importbenefitService;

    @Autowired
    private ILoginService loginService;

    /**
     * 查询续费记录
列表
     */
    @PreAuthorize(hasPermi = "system:importbenefit:list")
    @GetMapping("/list")
    public TableDataInfo list(Importbenefit importbenefit)
    {
        startPage();
        List<Importbenefit> list = importbenefitService.selectImportbenefitList(importbenefit);
        return getDataTable(list);
    }

    /**
     * 导出续费记录
列表
     */
    @PreAuthorize(hasPermi = "system:importbenefit:export")
    @Log(title = "续费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Importbenefit importbenefit) throws IOException
    {
        List<Importbenefit> list = importbenefitService.selectImportbenefitList(importbenefit);
        ExcelUtil<Importbenefit> util = new ExcelUtil<Importbenefit>(Importbenefit.class);
        util.exportExcel(response, list, "续费记录数据");
    }

    /**
     * 获取续费记录
详细信息
     */
    @PreAuthorize(hasPermi = "system:importbenefit:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(importbenefitService.selectImportbenefitById(recordId));
    }

    /**
     * 新增续费记录

     */
    @PreAuthorize(hasPermi = "system:importbenefit:add")
    @Log(title = "续费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Importbenefit importbenefit)
    {
        return toAjax(importbenefitService.insertImportbenefit(importbenefit));
    }

    /**
     * 修改续费记录

     */
    @PreAuthorize(hasPermi = "system:importbenefit:edit")
    @Log(title = "续费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Importbenefit importbenefit)
    {
        return toAjax(importbenefitService.updateImportbenefit(importbenefit));
    }

    /**
     * 删除续费记录

     */
    @PreAuthorize(hasPermi = "system:importbenefit:remove")
    @Log(title = "续费记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(importbenefitService.deleteImportbenefitByIds(recordIds));
    }

    /**
     * 后端——会员续费记录
     * */
    @PostMapping("/backImportbenefit")
    public AjaxResult backSelectImportbenefit(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (customerId != null && customerId != 0){
            List<ImportbenefitVo> importbenefitVos;
            try {
                importbenefitVos = importbenefitService.backSelectImportbenefit(customerId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (importbenefitVos != null && importbenefitVos.size() > 0){
                return AjaxResult.success(importbenefitVos);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
